﻿using System;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Collections.Generic;

namespace XQ.Framework.core
{
    /// <summary>
    /// 类型帮助类
    /// </summary>
    public class ConvertHelper
    {
        #region 字符串转换
        public static string getString(object obj)
        {
            return getString(obj, string.Empty);
        }
        public static string getString(object obj, string str)
        {
            return (obj == null) ? str : obj.ToString();
        }
        #endregion

        #region 数字转换
        //int
        public static int getInt(object obj)
        {
            return getInt(obj, default(int));
        }
        public static int getInt(object obj, int deft)
        {
            try
            {
                return obj == null ? deft : int.Parse(obj.ToString());
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        //float
        public static float getFloat(object obj)
        {
            return getFloat(obj, default(float));
        }
        public static float getFloat(object obj, float deft)
        {
            try
            {
                return obj == null ? deft : float.Parse(obj.ToString());
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        //long
        public static long getLong(object obj)
        {
            return getLong(obj, default(long));
        }
        public static long getLong(object obj, long deft)
        {
            try
            {
                return obj == null ? deft : long.Parse(obj.ToString());
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        //double
        public static double getDouble(object obj)
        {
            return getDouble(obj, default(double));
        }
        public static double getDouble(object obj, double deft)
        {
            try
            {
                return obj == null ? deft : double.Parse(obj.ToString());
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        //decimal
        public static decimal getDecimal(object obj)
        {
            return getDecimal(obj, default(decimal));
        }
        public static decimal getDecimal(object obj, decimal deft)
        {
            try
            {
                return obj == null ? deft : decimal.Parse(obj.ToString());
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        #endregion

        #region 日期转换
        public static DateTime getDateTime(object obj, DateTime deft)
        {
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(ConvertHelper.getString(obj), out dt))
            {
                return dt;
            }
            return deft;
            /*
            try
            {
                return DateTime.Parse(ConvertHelper.getString(obj));
            }
            catch (Exception e)
            {
                return deft;
            }*/
        }
        public static DateTime getDateTime(object obj)
        {
            return getDateTime(obj, DateTime.Now);
        }
        #endregion

        #region 图片转换
        public static byte[] BitmapToBytes(Bitmap bm)
        {
            return BitmapToBytes(bm, ImageFormat.Png);
        }
        public static byte[] BitmapToBytes(Bitmap bm, ImageFormat format)
        {
            MemoryStream ms = null;
            try
            {
                if (bm == null) { return null; }
                ms = new MemoryStream();
                bm.Save(ms, format);
                return ms.ToArray();
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                if (bm != null) { bm.Dispose(); }
                if (ms != null) { ms.Close(); }
            }
        }

        public static Bitmap BytesToBitmap(byte[] buffer)
        {
            if (!ValidateHelper.IsPlumpArray<byte>(buffer)) { return null; }
            MemoryStream ms = new MemoryStream();
            try
            {
                ms.Write(buffer, 0, buffer.Length);
                Bitmap bmp = new Bitmap(ms);
                return bmp;
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                ms.Close(); ms.Dispose();
            }
        }
        public static string BytesToBase64(byte[] b)
        {
            try { return Convert.ToBase64String(b); }
            catch (Exception e) { return null; }
        }
        public static byte[] Base64ToBytes(string b64)
        {
            try { return Convert.FromBase64String(b64); }
            catch (Exception e) { return null; }
        }
        public static Bitmap ImgFromBase64(string Img)
        {
            return BytesToBitmap(Base64ToBytes(Img));
        }
        /// <summary>
        /// 将字符串编码为Base64字符串
        /// </summary>
        /// <param name="str">要编码的字符串</param>
        public static string Base64Encode(string str)
        {
            byte[] barray;
            barray = Encoding.Default.GetBytes(str);
            return Convert.ToBase64String(barray);
        }

        /// <summary>
        /// 将Base64字符串解码为普通字符串
        /// </summary>
        /// <param name="str">要解码的字符串</param>
        public static string Base64Decode(string str)
        {
            byte[] barray;
            barray = Convert.FromBase64String(str);
            return Encoding.Default.GetString(barray);
        }
        /// <summary>
        /// 使用stream的length来定义数组，只有stream可以查找(seek)的情况下使用
        /// 用完不会关闭流
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            if (stream == null || !stream.CanRead) { return null; }
            if (stream.CanSeek)
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                return bytes;
            }
            else
            {
                List<byte> bytes = new List<byte>();
                int b = 0;
                while (true)
                {
                    if ((b = stream.ReadByte()) == -1)
                    {
                        break;
                    }
                    bytes.Add((byte)b);
                }
                return bytes.ToArray();
            }
        }
        #endregion

        #region 通用转换
        public static T ConvertType<T>(object obj)
        {
            return ConvertType<T>(obj, default(T));
        }
        public static T ConvertType<T>(object obj, T deft)
        {
            try
            {
                return obj == null ? deft : (T)Convert.ChangeType(obj, typeof(T));
            }
            catch (Exception e)
            {
                return deft;
            }
        }
        #endregion
    }

}
